<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="org.apache.commons.logging.*" %>
<%@ page import="weaver.conn.RecordSet" %>
<jsp:useBean id="rs1" class="weaver.conn.RecordSet" scope="page" />
<jsp:useBean id="rs2" class="weaver.conn.RecordSet" scope="page" />
<jsp:useBean id="sysRemind" class="weaver.system.SysRemindWorkflow" scope="page"/>

<%
	
	Log log = LogFactory.getLog(this.getClass());
	log.info("外勤签到申请提醒开始");
	String userid = Util.null2String(request.getParameter("userid"));
	String currentDate = Util.null2String(request.getParameter("currentDate"));
	String currentTime = Util.null2String(request.getParameter("currentTime"));
	String card = Util.null2String(request.getParameter("card"));
	String cardsj = Util.null2String(request.getParameter("cardsj"));
	String Title = "外勤打卡申请("+GetName(userid)+")"+currentDate+" "+currentTime;
	//查询用户所在部门的部门等级，部门ID,职级，部门负责人等信息
	String sql = "select dep.depgrade,dep.deptid,hrm.joblevel,dep.directorI,dep.directorII,dep.directorIII from HrmDepartmentDefined as dep left join HrmResource as hrm on dep.deptid=hrm.departmentid where hrm.id=?";
	rs1.executeQuery(sql,userid);
	String depgrade = "";
	String deptid = "";
	String joblevel = "";
	String directorI = "";
	String directorII = "";
	String directorIII = "";
	String depManager = "";
	while(rs1.next()){
		depgrade = rs1.getString("depgrade");
		deptid = rs1.getString("deptid");
		joblevel = rs1.getString("joblevel");
		directorI = rs1.getString("directorI");
		directorII = rs1.getString("directorII");
		directorIII = rs1.getString("directorIII");
		
		String sql2 = "select id from hrmresource where id=CONCAT('',(select (case depgrade when 3 then  directorIII when 2  then directorII when 1 then directorII end)"; 
		   sql2 += " as bmfzr from HrmDepartmentDefined where  depgrade="+depgrade+" and deptid="+deptid+" ))";
		rs2.execute(sql2);
		if(rs2.next()){
			depManager = rs2.getString("id");
		}
	}
	
	log.info("用户："+GetName(userid)+" 部门ID："+deptid+" 部门等级："+depgrade+" 职级："+joblevel);
	log.info("该部门矩阵负责人："+" 分管领导："+directorI+" 二级部门负责人："+directorII+" 三级部门负责人："+directorIII);
	boolean flag=false;
	Boolean result = CompareIsDepManager(userid,depManager);
	log.info("result="+result);
	//如果是部门负责人
	if(result){
		if("3".equals(depgrade)){	//发送给二级部门负责人
			try {
				sysRemind.setCRMSysRemind(Title,0 , Util.getIntValue(userid), directorII, Title);
				flag=true;
			} catch (Exception e1) {
				flag=false;
				e1.printStackTrace();
			}
		}
		else if("2".equals(depgrade)){	//发送给分管领导
			try {
				sysRemind.setCRMSysRemind(Title,0 , Util.getIntValue(userid), directorI, Title);
				flag=true;
			} catch (Exception e1) {
				flag=false;
				e1.printStackTrace();
			}
		}
	}
	//不是部门负责人
	else{
		if("3".equals(depgrade)){	//发送给三级部门负责人
			try {
				sysRemind.setCRMSysRemind(Title,0 , Util.getIntValue(userid), directorIII, Title);
				flag=true;
			} catch (Exception e1) {
				flag=false;
				e1.printStackTrace();
			}
		}
		else if("2".equals(depgrade)){	//发送给二级部门负责人
			try {
				sysRemind.setCRMSysRemind(Title,0 , Util.getIntValue(userid), directorII, Title);
				flag=true;
			} catch (Exception e1) {
				flag=false;
				e1.printStackTrace();
			}
		}
	}
	
	
	/**
	try {
		sysRemind.setCRMSysRemind(Title,0 , Util.getIntValue(userid), depManager, Title);
	} catch (Exception e1) {	
		e1.printStackTrace();
	}
	**/
	RecordSet rs=new RecordSet();
	String sqlstr="insert into uf_wqsq(sqrid,sqrq,sqcard,cardsj,sqdatetime)values(?,?,?,?,?)";
	boolean inflag=rs.executeUpdate(sqlstr, userid,currentDate,card,cardsj,currentDate+" "+currentTime);
	if(inflag&&flag){
		out.print("1");
	}else{
		out.print("0");
	}
	
	log.info("外勤签到申请提醒结束");
%>

<%!
	/**
		获取用户名
	**/
	public static String GetName(String userid){
		
		RecordSet rs = new RecordSet();
		String sql = "select  lastname from HrmResource where id="+userid;
		rs.execute(sql);
		String Name = "";
		if(rs.next()){
			Name = rs.getString("lastname");
		}
		return Name;
	}
	/**
		判断当前用户是否是部门负责人
		返回true代表是
	**/
	public static Boolean CompareIsDepManager(String userid,String depManager){
		Boolean flag = true;
		if(Util.getIntValue(userid)!=Util.getIntValue(depManager)){
			flag = false;
		}
		else{
			flag = true;
		}
		return flag;
	}
%>
